package com.zs.lib_base.utils


import android.annotation.SuppressLint
import android.util.Log
import java.text.SimpleDateFormat
import java.util.*


/**
 *
 *on 2020/11/6 0006
 */
class DateTimeUtils {
    companion object {
        const val DateYear = "yyyy"
        const  val DateMonth = "MM"
        const val DateDay = "dd"
        const val DateHour = "HH"
        const val DateMinute = "mm"
        const val DateSecond = "ss"

        const val DateSimple = "yyyy-MM-dd"
        const val DateYHS = "yyyy-MM-dd HH:mm:ss"
        const val DateHMS = "HH:mm:ss"


        @SuppressLint("SimpleDateFormat")
        fun getTime(date: Date, format: String): String { //可根据需要自行截取数据显示
            Log.d("getTime()", "choice date millis: " + date.time)
            val value = SimpleDateFormat(format)
            return value.format(date)
        }

        fun getDate(time: Long): String { //可根据需要自行截取数据显示
            return SimpleDateFormat("HH:mm:ss", Locale.CHINA).format(Date(time))
        }

        @SuppressLint("SimpleDateFormat")
        fun getDateToFormat(time: String, format: String): Long {
            val simpleDateFormat = SimpleDateFormat(format)
            val date = Date(System.currentTimeMillis())
            val currentTime: String = simpleDateFormat.format(date)

            val begin: Date = simpleDateFormat.parse(currentTime)!!
            val end: Date = simpleDateFormat.parse(time)!!
            return end.time - begin.time
        }

        fun getHour(time: Long): String {
            /*计算小时*/
            val hours: Long = (time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
            if (hours < 10) {
                return "0$hours"
            }
            return hours.toString()
        }

        fun getMinutes(time: Long): String {
            /*计算分钟*/
            val minutes: Long = (time % (1000 * 60 * 60)) / (1000 * 60)
            if (minutes < 10) {
                return "0$minutes"
            }
            return minutes.toString()
        }

        fun getSeconds(time: Long): String {
            /*计算秒*/
            val seconds: Long = (time % (1000 * 60)) / 1000
            if (seconds < 10) {
                return "0$seconds"
            }
            return seconds.toString()
        }

        private const val minute = 60 * 1000 // 1分钟
            .toLong()
        private const val hour = 60 * minute // 1小时

        private const val day = 24 * hour // 1天

        private const val month = 31 * day // 月

        private const val year = 12 * month // 年
        fun getDateByType(time: Long, type: String): String {
            return SimpleDateFormat(type, Locale.CHINA).format(Date(time))
        }

        @SuppressLint("SimpleDateFormat")
        fun getTimeFormatText(time: String): String? {
            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //小写的mm表示的是分钟
            val date = sdf.parse(time) ?: return null

            val diff = Date().time - date.time
            var r: Long = 0
            if (diff > year) {
                time
            }
            if (diff > month) {
                time
            }
            if (diff > day) {
                r = diff / day
                return if (r <= 3) {
                    r.toString() + "天前"
                } else {
                    time
                }
            }
            if (diff > hour) {
                r = diff / hour
                return r.toString() + "小时前"
            }
            if (diff > minute) {
                r = diff / minute
                return r.toString() + "分钟前"
            }
            return "刚刚"
        }

        @SuppressLint("SimpleDateFormat")
        fun getImTimeFormatText(time: Long): String? {
            val dateStr = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(Date(time))

            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val date = sdf.parse(dateStr) ?: return null

            val diff = Date().time - date.time
            var r: Long = 0
//            if (diff > year) {
//                dateStr
//            }
//            if (diff > month) {
//                dateStr
//            }
//            if (diff > day) {
//                dateStr
//            }
//            if (diff > day) {
//                r = diff / day
//                return if (r <= 3) {
//                    r.toString() + "天前"
//                } else {
//                    time
//                }
//            }
            if (diff > hour) {
                r = diff / hour
                return if (r > 12) {
                    dateStr
                } else {
                    r.toString() + "小时前"
                }

            }
            if (diff > minute) {
                r = diff / minute
                return r.toString() + "分钟前"
            }
            return "刚刚"
        }

        @SuppressLint("SimpleDateFormat")
        fun getRealTimeFormatText(time: String): String? {
            val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss") //小写的mm表示的是分钟
            val date = sdf.parse(time) ?: return null

//            val date = Date(time)
            val diff = Date().time - date.time
            var r: Long = 0
            if (diff > year) {
                time
            }
            if (diff > month) {
                time
            }
            if (diff > day) {
                r = diff / day
                return if (r <= 3) {
                    r.toString() + "天前"
                } else {
                    time
                }
            }
            if (diff > hour) {
                r = diff / hour
                return r.toString() + "小时前"
            }
            if (diff > minute) {
                r = diff / minute
                return r.toString() + "分钟前"
            }
            return "刚刚"
        }

        fun getOverTime(): Long {
            val calendar: Calendar = Calendar.getInstance()
            calendar.add(Calendar.DAY_OF_YEAR, 1)
            calendar.set(Calendar.HOUR_OF_DAY, 0)
            calendar.set(Calendar.MINUTE, 0)
            calendar.set(Calendar.SECOND, 0)
            calendar.set(Calendar.MILLISECOND, 0)
            return (calendar.timeInMillis - System.currentTimeMillis())
        }
    }

    // 将字符串转为时间戳
    @SuppressLint("SimpleDateFormat")
    fun getTime(user_time: String, format: String): String? {
        var re_time: String? = null
        val sdf = SimpleDateFormat(format)
        val d: Date
        try {
            d = sdf.parse(user_time)!!
            val l = d.time
            val str = l.toString()
            re_time = str.substring(0, 10)
        } catch (e: Exception) {
        }
        return re_time
    }

}